<template>
    <div class="app-container">
        <div class="search-container">
            <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
                <el-form-item label="主键" prop="outpFeesMasterId">
                    <el-input v-model="queryParams.outpFeesMasterId" clearable placeholder="请输入主键"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="病人信息Id" prop="patientId">
                    <el-input v-model="queryParams.patientId" clearable placeholder="请输入病人信息Id"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="就诊ID" prop="clinicId">
                    <el-input v-model="queryParams.clinicId" clearable placeholder="请输入就诊ID"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="就诊日期" prop="visitDate">
                    <el-date-picker v-model="queryParams.visitDate" clearable placeholder="请选择就诊日期" type="date"
                                    value-format="YYYY-MM-DD">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="待收费记录ID" prop="ordersId">
                    <el-input v-model="queryParams.ordersId" clearable placeholder="请输入待收费记录ID"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="收费号" prop="rcptNo">
                    <el-input v-model="queryParams.rcptNo" clearable placeholder="请输入收费号"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="${comment}" prop="serialNoParent">
                    <el-input v-model="queryParams.serialNoParent" clearable placeholder="请输入${comment}"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="流水号" prop="serialNo">
                    <el-input v-model="queryParams.serialNo" clearable placeholder="请输入流水号"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="总费用" prop="totalCosts">
                    <el-input v-model="queryParams.totalCosts" clearable placeholder="请输入总费用"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="应收费用" prop="totalCharges">
                    <el-input v-model="queryParams.totalCharges" clearable placeholder="请输入应收费用"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="舍入差额" prop="diffCost">
                    <el-input v-model="queryParams.diffCost" clearable placeholder="请输入舍入差额"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="缴费标志" prop="chargeFlag">
                    <el-select v-model="queryParams.chargeFlag" clearable placeholder="请选择缴费标志">
                        <el-option v-for="dict in dictDataMap['charge_flag_dict']" :key="dict.dictValue"
                                   :label="dict.dictLabel"
                                   :value="dict.dictValue"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="退费号" prop="refundedFeesNo">
                    <el-input v-model="queryParams.refundedFeesNo" clearable placeholder="请输入退费号"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="退费人" prop="refundedPersonName">
                    <el-input v-model="queryParams.refundedPersonName" clearable placeholder="请输入退费人"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="结账号" prop="acctNo">
                    <el-input v-model="queryParams.acctNo" clearable placeholder="请输入结账号"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="开单医生" prop="applyDoctor">
                    <el-input v-model="queryParams.applyDoctor" clearable placeholder="请输入开单医生"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="开单科室" prop="applyDept">
                    <el-input v-model="queryParams.applyDept" clearable placeholder="请输入开单科室"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="执行科室" prop="implementDept">
                    <el-input v-model="queryParams.implementDept" clearable placeholder="请输入执行科室"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="执行人" prop="implementUser">
                    <el-input v-model="queryParams.implementUser" clearable placeholder="请输入执行人"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="机构ID" prop="orgId">
                    <el-input v-model="queryParams.orgId" clearable placeholder="请输入机构ID"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="备注信息" prop="remarks">
                    <el-input v-model="queryParams.remarks" clearable placeholder="请输入备注信息"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="医保结算交易类型" prop="ybSerialType">
                    <el-select v-model="queryParams.ybSerialType" clearable placeholder="请选择医保结算交易类型">
                        <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue"
                                   :label="dict.dictLabel"
                                   :value="dict.dictValue"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="医保结算交易流水号" prop="ybSerialNo">
                    <el-input v-model="queryParams.ybSerialNo" clearable placeholder="请输入医保结算交易流水号"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="医保结算交易信息" prop="ybSerialData">
                    <el-input v-model="queryParams.ybSerialData" clearable placeholder="请输入医保结算交易信息"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="电子发票流水号" prop="invoiceSerialNo">
                    <el-input v-model="queryParams.invoiceSerialNo" clearable placeholder="请输入电子发票流水号"
                              @keyup.enter="handleQuery"/>
                </el-form-item>
                <el-form-item label="参保类型" prop="canBaoType">
                    <el-select v-model="queryParams.canBaoType" clearable placeholder="请选择参保类型">
                        <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue"
                                   :label="dict.dictLabel"
                                   :value="dict.dictValue"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
                </el-form-item>
            </el-form>
        </div>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button v-hasPerm="['finance:outpFeesMaster:add']" plain type="primary" @click="handleAdd">
                    <template
                        #icon>
                        <i-ep-plus/>
                    </template>
                    新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['finance:outpFeesMaster:edit']" :disabled="single" plain type="success"
                           @click="handleUpdate">
                    <template #icon>
                        <i-ep-edit/>
                    </template>
                    修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['finance:outpFeesMaster:remove']" :disabled="multiple" plain type="danger"
                           @click="handleDelete">
                    <template #icon>
                        <i-ep-delete/>
                    </template>
                    删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['finance:outpFeesMaster:export']" plain type="warning" @click="handleExport">
                    <template
                        #icon>
                        <i-ep-download/>
                    </template>
                    导出
                </el-button>
            </el-col>
        </el-row>

        <el-table v-adaptive :border="true" :data="outpFeesMasterList" :header-cell-style="$headerCellStyle" :stripe="true"
                  highlight-current-row @selection-change="handleSelectionChange">
            <el-table-column :align="'center'" type="selection" width="55"/>
            <el-table-column :align="'center'" label="主键" prop="outpFeesMasterId"/>
            <el-table-column :align="'center'" label="病人信息Id" prop="patientId"/>
            <el-table-column :align="'center'" label="就诊ID" prop="clinicId"/>
            <el-table-column :align="'center'" label="就诊日期" prop="visitDate" width="180">
                <template #default="scope">
                    <span>{{ $parseTime(scope.row.visitDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="待收费记录ID" prop="ordersId"/>
            <el-table-column :align="'center'" label="收费号" prop="rcptNo"/>
            <el-table-column :align="'center'" label="${comment}" prop="serialNoParent"/>
            <el-table-column :align="'center'" label="流水号" prop="serialNo"/>
            <el-table-column :align="'center'" label="总费用" prop="totalCosts"/>
            <el-table-column :align="'center'" label="应收费用" prop="totalCharges"/>
            <el-table-column :align="'center'" label="舍入差额" prop="diffCost"/>
            <el-table-column :align="'center'" label="缴费标志" prop="chargeFlag">
                <template #default="scope">
                    <dict-data-tag :options="dictDataMap['charge_flag_dict']" :value="scope.row.chargeFlag"/>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="退费号" prop="refundedFeesNo"/>
            <el-table-column :align="'center'" label="退费人" prop="refundedPersonName"/>
            <el-table-column :align="'center'" label="结账号" prop="acctNo"/>
            <el-table-column :align="'center'" label="开单医生" prop="applyDoctor"/>
            <el-table-column :align="'center'" label="开单科室" prop="applyDept"/>
            <el-table-column :align="'center'" label="执行科室" prop="implementDept"/>
            <el-table-column :align="'center'" label="执行人" prop="implementUser"/>
            <el-table-column :align="'center'" label="机构ID" prop="orgId"/>
            <el-table-column :align="'center'" label="备注信息" prop="remarks"/>
            <el-table-column :align="'center'" label="医保结算交易类型" prop="ybSerialType"/>
            <el-table-column :align="'center'" label="医保结算交易流水号" prop="ybSerialNo"/>
            <el-table-column :align="'center'" label="医保结算交易信息" prop="ybSerialData"/>
            <el-table-column :align="'center'" label="电子发票流水号" prop="invoiceSerialNo"/>
            <el-table-column :align="'center'" label="参保类型" prop="canBaoType"/>
            <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作" width="220">
                <template #default="scope">
                    <el-button v-hasPerm="['finance:outpFeesMaster:edit']" type="success"
                               @click="handleUpdate(scope.row)">
                        <template #icon>
                            <i-ep-edit/>
                        </template>
                        修改
                    </el-button>
                    <el-button v-hasPerm="['finance:outpFeesMaster:remove']" type="danger"
                               @click="handleDelete(scope.row)">
                        <template #icon>
                            <i-ep-delete/>
                        </template>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                    :total="total" @pagination="getList"/>

        <!-- 添加或修改门诊收费记录主对话框 -->
        <el-dialog v-model="open" v-diagmove :close-on-click-modal="false" :title="title" append-to-body width="500px">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="主键" prop="outpFeesMasterId">
                    <el-input v-model="form.outpFeesMasterId" placeholder="请输入主键"/>
                </el-form-item>
                <el-form-item label="病人信息Id" prop="patientId">
                    <el-input v-model="form.patientId" placeholder="请输入病人信息Id"/>
                </el-form-item>
                <el-form-item label="就诊ID" prop="clinicId">
                    <el-input v-model="form.clinicId" placeholder="请输入就诊ID"/>
                </el-form-item>
                <el-form-item label="就诊日期" prop="visitDate">
                    <el-date-picker v-model="form.visitDate" clearable placeholder="请选择就诊日期"
                                    type="datetime"
                                    value-format="YYYY-MM-DD HH:mm:ss">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="待收费记录ID" prop="ordersId">
                    <el-input v-model="form.ordersId" placeholder="请输入待收费记录ID"/>
                </el-form-item>
                <el-form-item label="收费号" prop="rcptNo">
                    <el-input v-model="form.rcptNo" placeholder="请输入收费号"/>
                </el-form-item>
                <el-form-item label="${comment}" prop="serialNoParent">
                    <el-input v-model="form.serialNoParent" placeholder="请输入${comment}"/>
                </el-form-item>
                <el-form-item label="流水号" prop="serialNo">
                    <el-input v-model="form.serialNo" placeholder="请输入流水号"/>
                </el-form-item>
                <el-form-item label="总费用" prop="totalCosts">
                    <el-input v-model="form.totalCosts" placeholder="请输入总费用"/>
                </el-form-item>
                <el-form-item label="应收费用" prop="totalCharges">
                    <el-input v-model="form.totalCharges" placeholder="请输入应收费用"/>
                </el-form-item>
                <el-form-item label="舍入差额" prop="diffCost">
                    <el-input v-model="form.diffCost" placeholder="请输入舍入差额"/>
                </el-form-item>
                <el-form-item label="缴费标志" prop="chargeFlag">
                    <el-select v-model="form.chargeFlag" placeholder="请选择缴费标志">
                        <el-option v-for="dict in dictDataMap['charge_flag_dict']" :key="dict.dictValue"
                                   :label="dict.dictLabel"
                                   :value="dict.dictValue"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="退费号" prop="refundedFeesNo">
                    <el-input v-model="form.refundedFeesNo" placeholder="请输入退费号"/>
                </el-form-item>
                <el-form-item label="退费人" prop="refundedPersonName">
                    <el-input v-model="form.refundedPerson" placeholder="请输入退费人Name"/>
                </el-form-item>
                <el-form-item label="结账号" prop="acctNo">
                    <el-input v-model="form.acctNo" placeholder="请输入结账号"/>
                </el-form-item>
                <el-form-item label="开单医生" prop="applyDoctor">
                    <el-input v-model="form.applyDoctor" placeholder="请输入开单医生"/>
                </el-form-item>
                <el-form-item label="开单科室" prop="applyDept">
                    <el-input v-model="form.applyDept" placeholder="请输入开单科室"/>
                </el-form-item>
                <el-form-item label="执行科室" prop="implementDept">
                    <el-input v-model="form.implementDept" placeholder="请输入执行科室"/>
                </el-form-item>
                <el-form-item label="执行人" prop="implementUser">
                    <el-input v-model="form.implementUser" placeholder="请输入执行人"/>
                </el-form-item>
                <el-form-item label="机构ID" prop="orgId">
                    <el-input v-model="form.orgId" placeholder="请输入机构ID"/>
                </el-form-item>
                <el-form-item label="备注信息" prop="remarks">
                    <el-input v-model="form.remarks" placeholder="请输入备注信息"/>
                </el-form-item>
                <el-form-item label="医保结算交易类型" prop="ybSerialType">
                    <el-select v-model="form.ybSerialType" placeholder="请选择医保结算交易类型">
                        <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue"
                                   :label="dict.dictLabel"
                                   :value="dict.dictValue"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="医保结算交易流水号" prop="ybSerialNo">
                    <el-input v-model="form.ybSerialNo" placeholder="请输入医保结算交易流水号"/>
                </el-form-item>
                <el-form-item label="医保结算交易信息" prop="ybSerialData">
                    <el-input v-model="form.ybSerialData" placeholder="请输入医保结算交易信息"/>
                </el-form-item>
                <el-form-item label="电子发票流水号" prop="invoiceSerialNo">
                    <el-input v-model="form.invoiceSerialNo" placeholder="请输入电子发票流水号"/>
                </el-form-item>
                <el-form-item label="参保类型" prop="canBaoType">
                    <el-select v-model="form.canBaoType" placeholder="请选择参保类型">
                        <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue"
                                   :label="dict.dictLabel"
                                   :value="dict.dictValue"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
        <span class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
export default {
    name: "OutpFeesMaster"
}
</script>

<script setup lang="ts">
import {
    addOutpFeesMaster,
    delOutpFeesMaster,
    getOutpFeesMaster,
    listOutpFeesMaster,
    updateOutpFeesMaster
} from "@/api/finance/outpFeesMaster";
import {useUserStoreHook} from "@/store/modules/user";
import {useDictStoreHook} from "@/store/modules/dict";

const dictStore = useDictStoreHook();
const useUserStore = useUserStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('charge_flag_dict'))

const {proxy} = getCurrentInstance() as any;
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const formRef = ref();
const queryParamsRef = ref()
const outpFeesMasterList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const queryParams = ref<OutpFeesMaster>({
    pageNum: 1,
    pageSize: 10,
    outpFeesMasterId: "",
    patientId: "",
    clinicId: "",
    visitDate: "",
    ordersId: "",
    rcptNo: "",
    serialNoParent: "",
    serialNo: "",
    totalCosts: 0,
    totalCharges: 0,
    diffCost: 0,
    chargeFlag: "",
    refundedFeesNo: "",
    refundedPerson: "",
    acctNo: "",
    applyDoctor: "",
    applyDept: "",
    implementDept: "",
    implementUser: "",
    orgId: "",
    remarks: "",
    ybSerialType: "",
    ybSerialNo: "",
    ybSerialData: "",
    invoiceSerialNo: "",
    canBaoType: ""
})
const data = ref({
    form: {
        outpFeesMasterId: "",
        patientId: "",
        clinicId: "",
        visitDate: "",
        ordersId: "",
        rcptNo: "",
        serialNoParent: "",
        serialNo: "",
        totalCosts: 0,
        totalCharges: 0,
        diffCost: 0,
        chargeFlag: "",
        refundedFeesNo: "",
        refundedPerson: "",
        acctNo: "",
        applyDoctor: "",
        applyDept: "",
        implementDept: "",
        implementUser: "",
        orgId: "",
        remarks: "",
        ybSerialType: "",
        ybSerialNo: "",
        ybSerialData: "",
        invoiceSerialNo: "",
        canBaoType: ""
    },

    rules: {
        outpFeesMasterId: [
            {
                required: true, message: "主键不能为空", trigger: "blur"
            }
        ],
        patientId: [
            {
                required: true, message: "病人信息Id不能为空", trigger: "blur"
            }
        ],
        rcptNo: [
            {
                required: true, message: "收费号不能为空", trigger: "blur"
            }
        ],
        totalCosts: [
            {
                required: true, message: "总费用不能为空", trigger: "blur"
            }
        ],
        totalCharges: [
            {
                required: true, message: "应收费用不能为空", trigger: "blur"
            }
        ],
        chargeFlag: [
            {
                required: true, message: "缴费标志不能为空", trigger: "change"
            }
        ],
        orgId: [
            {
                required: true, message: "机构ID不能为空", trigger: "blur"
            }
        ],
    }
});

const {form, rules} = toRefs(data.value);

/** 查询门诊收费记录主列表 */
function getList() {
    listOutpFeesMaster(queryParams.value).then((response: any) => {
        outpFeesMasterList.value = response.rows;
        total.value = response.total;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        outpFeesMasterId: "",
        patientId: "",
        clinicId: "",
        visitDate: "",
        ordersId: "",
        rcptNo: "",
        serialNoParent: "",
        serialNo: "",
        totalCosts: 0,
        totalCharges: 0,
        diffCost: 0,
        chargeFlag: "",
        refundedFeesNo: "",
        refundedPerson: "",
        acctNo: "",
        applyDoctor: "",
        applyDept: "",
        implementDept: "",
        implementUser: "",
        orgId: "",
        remarks: "",
        ybSerialType: "",
        ybSerialNo: "",
        ybSerialData: "",
        invoiceSerialNo: "",
        canBaoType: ""
    };
    nextTick(() => {
        formRef.value.resetFields()
    })
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    queryParamsRef.value.resetFields()
    // proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.outpFeesMasterId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    if (!queryParams.value.orgId) {
        return ElMessage.error("请先选择机构")
    }
    open.value = true;
    reset();
    title.value = "添加门诊收费记录主";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    open.value = true;
    reset();
    const _outpFeesMasterId = row.outpFeesMasterId || ids.value
    getOutpFeesMaster(_outpFeesMasterId).then((response: any) => {
        form.value = response.data;
        title.value = "修改门诊收费记录主";
    });
}

/** 提交按钮 */
function submitForm() {
    (formRef.value as any).validate(valid => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.outpFeesMasterId != null) {
                updateOutpFeesMaster(form.value).then((response: any) => {
                    ElMessage.success("修改成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            } else {
                addOutpFeesMaster(form.value).then((response: any) => {
                    ElMessage.success("新增成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const outpFeesMasterIds = row.outpFeesMasterId ||
        ids.value;
    ElMessageBox.confirm('是否确认删除门诊收费记录主编号为"' + outpFeesMasterIds + '"的数据项？',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        delOutpFeesMaster(outpFeesMasterIds).then(() => {
            getList();
            ElMessage.success("删除成功");
        })
    }).catch(() => {
    })
}


/** 导出按钮操作 */
function handleExport() {
    proxy.download('finance/outpFeesMaster/export', {
        ...queryParams.value
    }, `outpFeesMaster_${new Date().getTime()}.xlsx`)
}

getList();
</script>
